package com.xinke.module.system.controller.admin.notice;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.notice.*;
import com.xinke.module.system.model.entity.notice.NoticeDO;
import com.xinke.module.system.service.notice.NoticeService;

@Tag(name = "管理后台 - 通知公告")
@RestController
@RequestMapping("/system/notice")
@Validated
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @PostMapping("/create")
    @Operation(summary = "创建通知公告")
    @PreAuthorize("@ss.hasPermission('system:notice:create')")
    public Result<Long> createNotice(@Valid @RequestBody NoticeSaveReqVO createReqVO) {
        return success(noticeService.createNotice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新通知公告")
    @PreAuthorize("@ss.hasPermission('system:notice:update')")
    public Result<Boolean> updateNotice(@Valid @RequestBody NoticeSaveReqVO updateReqVO) {
        noticeService.updateNotice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通知公告")
    @Parameter(name = "noticeId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:notice:delete')")
    public Result<Boolean> deleteNotice(@RequestParam("noticeId") Long noticeId) {
        noticeService.deleteNotice(noticeId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通知公告")
    @Parameter(name = "noticeId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:notice:query')")
    public Result<NoticeRespVO> getNotice(@RequestParam("noticeId") Long noticeId) {
        NoticeDO notice = noticeService.getNotice(noticeId);
        return success(BeanUtils.toBean(notice, NoticeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得通知公告分页")
    @PreAuthorize("@ss.hasPermission('system:notice:query')")
    public Result<PageResult<NoticeRespVO>> getNoticePage(@Valid NoticePageReqVO pageReqVO) {
        PageResult<NoticeDO> pageResult = noticeService.getNoticePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NoticeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通知公告 Excel")
    @PreAuthorize("@ss.hasPermission('system:notice:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNoticeExcel(@Valid NoticePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NoticeDO> list = noticeService.getNoticePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "通知公告.xls", "数据", NoticeRespVO.class,
                        BeanUtils.toBean(list, NoticeRespVO.class));
    }

}